package cn.com.dingtek.genius.manager.server;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Scanner;

public class CodeGenerator {
    public static final String JDBC_URL = "jdbc:mysql://localhost:3306/genius?useUnicode=true&useSSL=false&characterEncoding=utf8";
    public static final String JDBC_USERNAME = "root";
    public static final String JDBC_PASSWORD = "peng.1234";
    public static final String PACKAGE_PARENT = "cn.com.dingtek.genius.manager.server.repository.db";

    public static String scanner(String tip) {
        System.out.println(new StringBuilder().append("请输入" + tip + "：").toString());

        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }

        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");

        AutoGenerator mpg = new AutoGenerator()
                .setGlobalConfig(new GlobalConfig()
                        .setOutputDir(projectPath + "/src/main/java")
                        .setOpen(false)
                        .setAuthor("mybatis plus")
                        .setIdType(IdType.AUTO)
                        .setFileOverride(true)
                        .setServiceImplName("%sManager")
                        .setDateType(DateType.ONLY_DATE))
                .setDataSource(new DataSourceConfig()
                        .setDriverName("com.mysql.cj.jdbc.Driver")
                        .setUrl(JDBC_URL)
                        .setUsername(JDBC_USERNAME)
                        .setPassword(JDBC_PASSWORD))
                .setPackageInfo(new PackageConfig()
                        .setParent(PACKAGE_PARENT)
                        .setServiceImpl("manager"))
                .setStrategy(new StrategyConfig()
                        .setNaming(NamingStrategy.underline_to_camel)
                        .setColumnNaming(NamingStrategy.underline_to_camel)
                        .setEntityLombokModel(true)
                        .setInclude(scanner("表名"))
                        .entityTableFieldAnnotationEnable(false)
                        .setLogicDeleteFieldName("valid")
                        .setVersionFieldName("version"))
                .setTemplate(new TemplateConfig()
                        .setEntity("/mybatis-templates/entity.java")
                        .setMapper("/mybatis-templates/mapper.java")
                        .setController(null)
                        .setService(null)
                        .setServiceImpl("/mybatis-templates/serviceImpl.java"))
                .setTemplateEngine(new FreemarkerTemplateEngine());

        mpg.execute();
    }
}